import datetime
import os

from flask import Flask, render_template, request, jsonify, send_from_directory
from flask_cors import CORS

from 政务网.通用审批.审批事项配置.审批事项未配置 import getItemCode, 获取业务信息
from 政务网.通用审批.审批事项配置.异常处理 import  start


app = Flask(__name__)
CORS(app)  # 启用CORS支持

@app.route('/')
def index():
    return render_template("index.html")


@app.route('/submit', methods=['POST'])
def submit():
    receive_number = request.json.get("receive_number")

    # 假设这里有一个函数获取业务信息
    reds = 获取业务信息(receive_number)
    re2=start(receive_number)

    if reds is not None:
        return jsonify(message=f"当前流水号:{receive_number}\n提交成功！事项配置结果是：{reds}\n提交成功！流程、业务补齐结果是：{re2}")
    else:
        return jsonify(message="未能获取业务信息，请检查流水号。")

@app.route('/home', methods=['GET', 'POST'])
def home():
    username = ""
    message = ""
    # 只保留核心逻辑：获取 username 并返回
    if request.method == 'POST':
        receive_number = request.form.get("receive_number")  # 设默认值，避免 None
        reds=获取业务信息(receive_number)
        # 异常处理结果=query(receive_number)


        message = f"提交成功！配置结果是：{reds}"


    return render_template("home.html", message=message)


@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.method == 'POST':
        receive_number = request.form.get("receive_number", "default_number")

        # 返回简单的测试数据
        test_data = {
            "status": "success",
            "number": 1,
            "result": "成功测试配置完成",
            "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }

        return jsonify(test_data)
    else:return {
        "status": "success",
        "number": 1,
        "result": "else返回测试配置完成",
        "timestamp": "2024-01-01 12:00:00"
    }

@app.route('/favicon.ico')
def favicon():
    return send_from_directory('./static/ico/ico.ico', mimetype='image/vnd.microsoft.icon')

if __name__ == '__main__':
    app.run(
        host='0.0.0.0',  # 必须设为 0.0.0.0，允许所有局域网设备访问
        port=5000,  # 用默认端口 5000（无需管理员权限，避免端口验证）
        debug=True  # 开发阶段开启（方便调试，局域网内风险低）
    )